﻿/*
 * This file is part of CommunitySettlers.
 *
 * CommunitySettlers is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * CommunitySettlers is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with CommunitySettlers.  If not, see <http://www.gnu.org/licenses/>.
 * 
 * Author: Christoph Husse
 * 
 * Also checkout our homepage: http://communitysettlers.codeplex.com/
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace SettlersEngine
{
    internal class ResourceStack : AutoPositionTranformable
    {
        private Image m_Element;
        private AnimationFrame[] m_Frames;

        public IResourceObject Resource { get; private set; }

        public ResourceStack(Exported.GameInstance inInstance, IResourceObject inResource)
            : base(inInstance, inResource)
        {
            Resource = inResource;
            Content = m_Element = new Image();
            m_Element.Visibility = System.Windows.Visibility.Hidden;
            m_Element.Width = 50;
            m_Element.Height = 50;

            Resource_OnResourceTypeChanged(inResource, inResource.ResourceType, inResource.ResourceType);

            Resource.OnResourceTypeChanged += Resource_OnResourceTypeChanged;
            Resource.OnCountChanged += Resource_OnCountChanged;
        }

        void Resource_OnResourceTypeChanged(IResourceObject inSender, Resource inOldValue, Resource inNewValue)
        {
            Animation anim = AnimatedElement.Library.FindClass(inNewValue.ToString()).AmbientSet.Animations[0];

            m_Frames = anim.Frames.ToArray();

            Resource_OnCountChanged(inSender, Resource.Count, Resource.Count);
        }

        void Resource_OnCountChanged(IResourceObject inSender, int inOldValue, int inNewValue)
        {
            if (inSender.Count == 0)
            {
                m_Element.Visibility = System.Windows.Visibility.Hidden;
            }
            else
            {
                try
                {
                    AnimationFrame frame = m_Frames[inSender.Count - 1];

                    m_Element.Source = frame.Source;
                    m_Element.Margin = new Thickness(frame.OffsetX, frame.OffsetY, 0, 0);
                    m_Element.Width = frame.Width;
                    m_Element.Height = frame.Height;
                    m_Element.Visibility = System.Windows.Visibility.Visible;
                }
                catch (IndexOutOfRangeException)
                {
                    throw new IndexOutOfRangeException("Animation for resource \"" + Resource.ResourceType + "\" does not support a stack size of " + inNewValue + ".");
                }
            }
        }
    }
}
